package com.cq.digitalemployee.user.controller;

import com.cq.digitalemployee.result.BaseResult;
import com.cq.digitalemployee.result.SuccessResult;
import com.cq.digitalemployee.user.entity.po.SysUser;
import com.cq.digitalemployee.user.entity.vo.SysUserVo;
import com.cq.digitalemployee.user.service.SysUserService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户数据操作控制类
 *
 * @author zzz
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    SysUserService sysUserService;

    @GetMapping("getAll")
    @RequiresPermissions("user:getAll")
    public BaseResult getAll() {
        BaseResult result = new SuccessResult();
        List<SysUserVo> sysUserVoList = sysUserService.findAll();
        result.setData(sysUserVoList);
        return result;
    }

    @GetMapping("get")
    @RequiresPermissions("user:get")
    public BaseResult get(String id) throws InvocationTargetException, IllegalAccessException {
        BaseResult result = new SuccessResult();
        SysUserVo sysUserVo = sysUserService.findById(id);
        result.setData(sysUserVo);
        return result;
    }

    @PostMapping("add")
    @RequiresPermissions("user:add")
    public BaseResult add(SysUser sysUser, HttpSession session) {
        SuccessResult successResult = new SuccessResult();
        if (sysUser.getId() == null || sysUser.getId().isEmpty()) {
            sysUser.setId(UUID.randomUUID().toString());
        }
        if (session.getAttribute("loginUserId") != null) {
            sysUser.setCreateUser((String) session.getAttribute("loginUserId"));
        } else {
            sysUser.setCreateUser("-1");
        }
        int insertResult = sysUserService.add(sysUser);
        successResult.setData(insertResult);
        return successResult;
    }

    @PostMapping("edit")
    @RequiresPermissions("user:edit")
    public BaseResult edit(SysUser sysUser, HttpSession session) {
        SuccessResult successResult = new SuccessResult();
        if (session.getAttribute("loginUserId") != null) {
            sysUser.setUpdateUser((String) session.getAttribute("loginUserId"));
        } else {
            sysUser.setUpdateUser("-1");
        }
        sysUser.setUpdateTime(new Date());
        int updateResult = sysUserService.update(sysUser);
        successResult.setData(updateResult);
        return successResult;
    }

    @DeleteMapping("delete")
    @RequiresPermissions("user:delete")
    public BaseResult delete(String id) {
        SuccessResult successResult = new SuccessResult();
        int updateResult = sysUserService.delete(id);
        successResult.setData(updateResult);
        return successResult;
    }
}